package com.iflysse.dao;

import com.iflysse.pojo.po.req.ChapterIdPoReq;
import com.iflysse.pojo.po.req.ChapterPoReq;
import com.iflysse.pojo.po.req.CourseIdPoReq;
import com.iflysse.pojo.po.resp.ChapterListPoResp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
 * 章节Dao
 */
@Repository
public interface ChapterDao {

    /**
     * 添加章
     *
     * @param chapterPoReq 章
     * @return
     */
    @Insert({"insert into tb_chapter (chapter_id,course_id,chapter_title) values (UUID(),#{courseId},#{chapterTitle})"})
    @Options(useGeneratedKeys = true, keyProperty = "chapter_id")
    int addChapter(ChapterPoReq chapterPoReq) throws SQLException;

    /**
     * 根据课程编号获取章列表
     *
     * @param courseIdPoReq 章列表
     * @return
     */
    @Select("select *  from tb_chapter where course_id = #{courseId} and is_delete = 0")
    @Results({
            @Result(property = "chapterId",column = "chapter_id"),
            @Result(property = "chapterTitle",column = "chapter_title"),
            @Result(property = "learningTime",column = "learning_time"),
    })
    ChapterListPoResp getChapterListByCourseId(CourseIdPoReq courseIdPoReq) throws SQLException;

    /**
     * 根据章节编号,查询该章下面的资源数量
     *
     * @param chapterIdPoReq
     * @return
     */
    @Select("select count(*) from tb_chapter_resource where chapter_id = #{chapterId}")
    Integer getResourceNumberOfChapter(ChapterIdPoReq chapterIdPoReq) throws SQLException;


    /**
     * 查询课程下面的章数量
     *
     * @param courseIdPoReq
     * @return
     */
    @Select("select count(*) count from tb_chapter where course_id = #{courseId} and is_delete = 0")
    int getAmountOfChapterByCourseId(CourseIdPoReq courseIdPoReq) throws SQLException;

    /**
     * 删除章
     * @param chapterIdPoReq
     * @return
     */
    @Update("update  tb_chapter set is_delete = 1 where chapter_id = #{chapterId}")
    int deleteChapterByChapterId(ChapterIdPoReq chapterIdPoReq) throws SQLException;

    /**
     * 根据章获取课程编号
     * @param chapterIdPoReq
     * @return
     */
    @Select("select course_id from tb_chapter where chapter_id =  #{chapterId}")
    String getCourseIdByChapterId(ChapterIdPoReq chapterIdPoReq) throws SQLException;

    /**
     * 根据资源获取章编号
     * @param resourceId
     * @return
     */
    @Select("select chapter_id from tb_chapter_resource where resource_id = #{resourceId}")
    String getChapterIdByResourceId(String resourceId) throws SQLException;

}